#Analysis with CHES data (Section B in the appendix)# 

sink(file = "log_file4.txt", type = "output")

require('tidyverse') # for simple data wrangling
require('rio')       # for simple data importing
require(magrittr)
require(dplyr)
require(stringr)
require(ggplot2)
require(tidyr)
require(MASS)
require(stargazer)
require(coefplot)
require(sjPlot)
require(margins)
require(plm)
require(estimatr)
require(lmtest)
require(lme4)
require(pscl)
require(jtools)
library(broom)      # For converting models into tables
library(rdrobust)   # For robust nonparametric regression discontinuity
library(rddensity) # For nonparametric regression discontinuity density tests
library(rddtools)
library(huxtable)   # For side-by-side regression tables
library(rdd)
require(haven)
require(plyr)
require(cowplot)
library(data.table)
require(gridExtra)
library(estimatr)
library(readr)
require(grid)
require(ggpubr)

# Data: CHES data (North Carolina Capel Hill)
# Period: 1999-2019


ches<-read.csv('CHESdata.csv')

#treatment 
ches$treatment<-ifelse(ches$dif_l1>=0,1,0)

#country FEs 
ches$state.f = factor(ches$country)
ches$state.d = model.matrix(~ches$state.f+0)


#drop radical right parties from the dataset
ches_rr<-ches%>%filter(!family=="Radical Right")


#descriptive statistics (Table A11)
stargazer(ches_rr)

#-----------------------------------------------------------------------------
# #INTERNATIONAL_SALIENCE = importance/salience of international security and peacekeeping missions. (Only asked in 2010)
#0 = Not important at all
#10 = Extremely important

#INTERNATIONAL_SALIENCE (negative=low, positive=high) 
#-----------------------------------------------------------------------------
#Table A11
ches1<-ches_rr%>%dplyr::select(dif_l1, international_salience, treatment,country,state.d,)
ches1<-ches1[complete.cases(ches1), ]
ches1$international_salience_l<-log(ches1$international_salience+1)
sal1<- rdd_data(x=dif_l1, y=international_salience_l,z=ches1$treatment, cutpoint=0, covar=ches1$state.d,data=ches1)
sal2 <- rdd_data(x=ches1$dif_l1, y=ches1$international_salience_l,z=ches1$treatment,cutpoint=0)

#parametric 
summary(ty<-rdd_reg_lm(rdd_object= sal1, covar.opt = list(strategy = c("include"), slope = c( "separate")), order=1 ))
coeftest(ty, vcov=vcovHC(ty,type="HC0",cluster="countryname"))
summary(ty2<-rdd_reg_lm(rdd_object= sal2, order=1 ))
coeftest(ty2, vcov=vcovHC(ty2,type="HC0",cluster="countryname"))


#-----------------------------------------------------------------------------
# #INTERNATIONAL_SECURITY = position towards international security and peacekeeping
#0 = Strongly favors COUNTRY troop deployment
#10 = Strongly opposes COUNTRY troop deployment

#INTERNATIONAL_security (negative=high, positive=low) 
#-----------------------------------------------------------------------------
#Table A12
ches2<-ches_rr%>%dplyr::select(dif_l1, international_security, treatment,country,state.d,)
ches2<-ches2[complete.cases(ches2), ]
ches2$international_security_l<-log(ches2$international_security+1)
pos1<- rdd_data(x=dif_l1, y=ches2$international_security_l,z=ches2$treatment, cutpoint=0, covar=ches2$state.d,data=ches2)
pos2 <- rdd_data(x=ches2$dif_l1, y=ches2$international_security_l,z=ches2$treatment,cutpoint=0)

summary(iy<-rdd_reg_lm(rdd_object= pos1, covar.opt = list(strategy = c("include"), slope = c( "separate")), order=1 ))
coeftest(iy, vcov=vcovHC(iy,type="HC0",cluster="countryname"))
summary(iy2<-rdd_reg_lm(rdd_object= pos2, order=1 ))
coeftest(iy2, vcov=vcovHC(iy2,type="HC0",cluster="countryname"))



sink()




